package unm

import unm.domain.Page
import unm.domain.PageSub

class Console {
    static void main(args) {
        //顺序不能颠倒
        generateLeft(0)
        generateCatalogContent()
        generateCollegeContent()
        //根目录下的页面
        generateLeft(0)
        generateHtml()
        //college下的页面
        generateLeft(2)
        generateCollegeDetailHtml()
    }

    static generateLeft(int levelNumber) {
        def left = new NameTool('left')
        def level = LevelTool.getLevel(levelNumber)
        File template = FileTool.getTemplate(left.templateName)
        File html = FileTool.getLeft(left.htmlName)
        List<Page> pages = DataLoader.page
        pages.each { page ->   // don't show test page in left bar
            PageSub test = page.pageSubs.find { it.pageName == 'test' }
            page.pageSubs.remove(test)
        }
        Map data = [pages: pages, level: level]
        TemplateTool.template2html(template, html, data)
    }

    static generateHtml() {
        File unmTemplate = FileTool.getTemplate('unm.template.html')
        File naviTemplate = FileTool.getTemplateNavi('navi.template.html')

        DataLoader.page.each { page ->
            page.pageSubs.each { sub ->
                if (sub.status != 'outlink') {
                    //---header---
                    def header = ''
                    if (sub.status == 'flexslider') {
                        header = FileTool.getHeader("flexslider.html").text
                    }
                    if (sub.status == 'colorbox') {
                        header = FileTool.getHeader("colorbox.html").text
                    }
                    //---left---
                    File left = FileTool.getLeft("left.html")
                    //---navi---
                    String navigator = TemplateTool.template2string(naviTemplate, [page: page, sub: sub])
                    //---content---
                    def subName = new NameTool(sub.pageName)
                    File content = FileTool.getContent(subName.contentName)
                    //---html--
                    File html = FileTool.getHtml(subName.htmlName)

                    def binding = [header: header, navigator: navigator, left: left.text,
                            content: content.text, level: LevelTool.getLevel(0)]
                    TemplateTool.template2html(unmTemplate, html, binding)
                }
            }
        }
    }

    static generateCollegeDetailHtml() {
        File unmTemplate = FileTool.getTemplate('unm.template.html')
        File naviTemplate = FileTool.getTemplateNavi('navi_college.template.html')
        File left = FileTool.getLeft("left.html")

        DataLoader.college.each { college ->
            File html = FileTool.getHtml(college.htmlName)
            File content = FileTool.getContent(college.contentHtmlName)
            String navigator = TemplateTool.template2string(naviTemplate, [college: college, depart: null])
            def binding = [header: '', navigator: navigator, left: left.text,
                    content: content.text, level: LevelTool.getLevel(2)]
            TemplateTool.template2html(unmTemplate, html, binding)

            college.departs.each { depart ->
                html = FileTool.getHtml(depart.htmlName)
                content = FileTool.getContent(depart.contentHtmlName)
                navigator = TemplateTool.template2string(naviTemplate, [college: college, depart: depart])

                binding = [header: '', navigator: navigator, left: left.text,
                        content: content.text, level: LevelTool.getLevel(2)]
                TemplateTool.template2html(unmTemplate, html, binding)
            }
        }
    }

    //--------------------利用excel生成的页面------------------------
    static generateCollegeContent() {
        def collegeContent = new NameTool('college.content')

        File template = FileTool.getTemplateContent(collegeContent.templateName)
        File html = FileTool.getContent(collegeContent.htmlName)
        Map data = [colleges: DataLoader.college]
        TemplateTool.template2html(template, html, data)
    }


    static generateCatalogContent() {
        def catalogContent = new NameTool('catalog.content')

        File template = FileTool.getTemplateContent(catalogContent.templateName)
        File html = FileTool.getContent(catalogContent.htmlName)
        Map data = [catalogss: DataLoader.catalog]
        TemplateTool.template2html(template, html, data)
    }

//    static initialCollegeFolderPage() {
//        List<CollegeExcel> colleges = CollegeExcel.load()
//        colleges.each { college ->
//            File f = FileTool.fileInContent(college.contentHtmlName)
//            if (!f.exists()) f.createNewFile()
//            f.write("<div class=\"title1\">${college.chineseName}<br>${college.englishName}</div><br><div class=\"center\">中文介绍敬请期待，请访问<a href=\"${college.url}\" target=\"_blank\">英文站点</a></div>")
//        }
//    }
}
